﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SQLite;
using DialogsEditor.DAO;


namespace DialogsEditor
{
    public partial class NewDialogForm : Form
    {
        public enum DialogType { PROJECT, DIALOG, }

        public static bool anyNewDialog = false;

        private Form1 main;
        private DialogType type;

        public NewDialogForm(Form1 form, DialogType t)
        {
            InitializeComponent();
            CenterToParent();
            Init();
            anyNewDialog = false;
            main = form;
            type = t;
        }

        private void Init()
        {
            using(SQLiteConnection conn = ConnFactory.getInstancia().GetConnLevel())
            {
                SQLiteDataReader reader = null;
                try
                {
                    conn.Open();
                    string select = "select nameTrigger from triggers";

                    SQLiteCommand cmd = new SQLiteCommand(select, conn);

                    reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        cbbTrigger.Items.Add(reader.GetString(0));
                    }
                }
                catch (NullReferenceException)
                {
                    MessageBox.Show("Erro de conexão");
                }
                catch(SQLiteException mysqlError)
                {
                    MessageBox.Show("Erro de acesso ao BD: " + mysqlError.Message, "Error");
                }
                finally
                {
                    if (reader != null) reader.Close();
                    conn.Close();
                }

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTitle.Text) ||
                string.IsNullOrEmpty(txtChar.Text) ||
                string.IsNullOrEmpty(cbbTrigger.Text) ||
                string.IsNullOrEmpty(txtInitialText.Text))
            {
                MessageBox.Show("Insira todas as informações!");
                return;
            }

            using (SQLiteConnection conn = ConnFactory.getInstancia().GetConnLevel())
            {
                string selectTriger = "SELECT id from triggers WHERE";
                if (DialogEditorDAO.GetId(conn, selectTriger, "nameTrigger", cbbTrigger.Text) <= 0)
                {
                    MessageBox.Show("Trigger inexistente!");
                    return;
                }
            }

            using (SQLiteConnection conn = ConnFactory.getInstancia().GetConnDialog())
            {
                string selectTitle = "select id from dialog WHERE";
                if (DialogEditorDAO.GetId(conn, selectTitle, "title", txtTitle.Text) > 0)
                {
                    MessageBox.Show("Já existe um dialogo com esse título!");
                    return;
                }
            }

            //Só vai entrar aqui, ou para criar um novo projeto, ou então quando o projeto atual não for nulo
            if (type == DialogType.DIALOG)
            {
                //isProj 1 == true and 0 == false

                /*using (SQLiteConnection conn = ConnFactory.getInstancia().GetConnLevel())
                {
                    //d.SetID(UtilsDAO.GetId(conn, "select id from dialog WHERE", "title", d.title.Text));

                    string selectTriger = "SELECT id from triggers WHERE";
                    if (UtilsDAO.GetId(conn, selectTriger, "nameTrigger", cbbTrigger.Text) <= 0)
                    {
                        MessageBox.Show("Trigger inexistente!");
                        return;
                    }
                }

                using (SQLiteConnection conn = ConnFactory.getInstancia().GetConnDialog())
                {
                    string selectTitle = "select id from dialog WHERE";
                    if (UtilsDAO.GetId(conn, selectTitle, "title", txtTitle.Text) > 0)
                    {
                        MessageBox.Show("Já existe um dialogo com esse título!");
                        return;
                    }
                }*/
                DialogEntity d = new DialogEntity(-1, txtTitle.Text, txtChar.Text, cbbTrigger.Text, txtInitialText.Text, -1, false, Form1.CurrentProject.GetID());
                //UtilsDAO.SaveDialog(d);
                Form1.CurrentDialogs.Add(d);
                main.ShowFormDialogs();
                anyNewDialog = true;
                Close();
            }
            else if (type == DialogType.PROJECT)
            {
                //isProj 1 == true and 0 == false
                DialogEntity proj = new DialogEntity(-1, txtTitle.Text, txtChar.Text, cbbTrigger.Text, txtInitialText.Text, -1, true, -1);
                DialogEditorDAO.SaveDialog(proj);

                using (SQLiteConnection conn = ConnFactory.getInstancia().GetConnDialog())
                {
                    proj.SetID(DialogEditorDAO.GetId(conn, "select id from dialog WHERE", "title", proj.title.Text));
                    proj.idProject = proj.GetID();
                }
                DialogEditorDAO.UpdateDialog(proj);
                Form1.CurrentProject = proj;
                main.ClearControlDialog();
                anyNewDialog = true;
                Close();
            }
        }
    }
}
